<template>
	<view class="page" :style="{'--color': color}">
		<status-bar title="城市经理申请"></status-bar>
		<view class="d-flex a-center" style="color: #169BD5;">
			<view class="title size30 m-right30" v-for="(item,index) in title" :key="index" @click="goBack(item)">{{ item.name }}</view>
		</view>
		<view class="list" v-for="(item,index) in pageData" :key="index">
			<view class="info">
				<text class="Name">{{ item.RegionName }}</text>
				<view class="size28" style="color: #FF0000;" v-if="item.agentCount > 0">{{ names[type] }}级代理已占用</view>
				<view v-else>
					<text class="size28 m-right20" style="color: #008000;">{{ names[type] }}级代理可用</text>
					<text class="size28 bold" style="color: #0066CC;" @click="handleApply(item)">立即申请</text>
				</view>
			</view>
			<text class="size28" style="color: #169BD5;" @click.stop="goNext(item)" v-if="type < 3">查看下级</text>
		</view>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	export default {
		data() {
			return {
				regioncode: 0,
				type: 1, // 1省 2市 3区 4街道
				pageData: [],
				title: [],
				names: {
					1: '省',
					2: '市',
					3: '区'
				}
			}
		},
		computed: {
			...mapState(['color'])
		},
		onLoad() {
			this.Division()
		},
		methods: {
			Division() {
				this.$http.post({
					url: '/singleSaleApi/getAdministrativeDivision',
					data: {
						regioncode: this.regioncode,
						type: this.type
					}
				}).then(res => {
					if(res.code != 200) return this.$Toast(res.msg)
					this.pageData = res.data || []
					this.title = res.title || []
				})
			},
			goNext(item) {
				this.regioncode = item.RegionCode
				this.type ++
				this.Division()
			},
			goBack(item) {
				this.type = item.type
				this.regioncode = item.code
				this.Division()
			},
			async handleApply(item) {
				const Flag = await this.$Modal('提示', `确定申请${item.RegionName}吗？`)
				if(!Flag) return

				this.$http.post({
					url: '/userAgentApi/editAgent',
					data: {
						Division1: item.Division1 || '',
						Division2: item.Division2 || '',
						Division3: item.Division3 || '',
						Division4: item.Division4 || ''
					}
				}).then(res => {
					this.$Toast(res.msg)
					this.Division()
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: $page;
		padding: 24rpx;
	}
	.title:not(:last-child) {
		position: relative;
		&::before {
			content: '>';
			font-size: 24rpx;
			position: absolute;
			right: -24rpx;
			top: 50%;
			transform: translateY(-50%);
		}
	}
	.list {
		width: 100%;
		padding: 24rpx;
		background-color: #fff;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		margin-top: 20rpx;
		.info {
			flex: 1;
			display: flex;
			align-items: center;
			.Name {
				max-width: 150rpx;
				font-size: 30rpx;
				flex-shrink: 0;
				margin-right: 20rpx;
			}
		}
	}
</style>
